<template>
  <el-dialog
    :title="curId ? '编辑用户' : '新增用户'"
    :visible.sync="dialogVisible"
    width="30%"
    :before-close="close"
  >

    <el-form ref="ruleForm" label-position="left  " :model="ruleForm" status-icon :rules="rules" label-width="100px" class="demo-ruleForm">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="ruleForm.username" clearable autocomplete="off" />
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input v-model="ruleForm.name" clearable autocomplete="off" />
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="ruleForm.mobile" clearable autocomplete="off" />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="ruleForm.email" clearable autocomplete="off" />
      </el-form-item>
      <el-form-item label="部门" prop="department">
        <treeselect
          v-model="ruleForm.department"
          :options="departmentsData"
          style="width: 178px"
          placeholder="选择部门"
        />
      </el-form-item>
      <el-form-item label="角色" prop="roles">
        <el-select v-model="ruleForm.roles" multiple placeholder="选择角色">
          <el-option
            v-for="item in rolesData"
            :key="item.name"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
// import the component
import { createUser, updateUser, getUser } from '@/api/system/user'
import { getRoles } from '@/api/system/roles'
// import { getDepartment } from '@/api/system/departments'
import Treeselect from '@riophae/vue-treeselect'
// import the styles
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
export default {
  name: 'UserFrom',
  components: { Treeselect },
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    curId: {
      type: Number,
      default: null
    },
    departmentsData: {
      type: Array,
      default: () => {
        return
      }
    }
  },
  data() {
    return {
      rolesData: [],
      ruleForm: {
        username: '',
        name: '',
        mobile: '',
        email: '',
        department: null,
        roles: null
      },
      rules: {
        username: [{ required: true, trigger: 'blur', message: '用户名不能为空' }],
        mobile: [{ pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号' }],
        email: [{ type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }]
      }
    }
  },
  watch: {
    dialogVisible(val) {
      if (val) {
        if (this.curId) {
          getUser(this.curId).then(res => {
            console.log('get user', res)
            this.ruleForm = res.data.results
          })
          this.getRoles()
        }
      }
    }
  },
  methods: {
    close() {
      this.$refs.ruleForm.resetFields()
      this.$emit('close')
    },
    userData() {
      this.$emit('user-data')
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.curId) {
            updateUser(this.curId, this.ruleForm).then(res => {
              console.log(res.data)
              this.$message({
                message: '修改用户信息成功',
                type: 'success'
              })
              this.close()
              this.userData()
            })
          } else {
            console.log('add user submit', this.ruleForm)
            createUser(this.ruleForm).then(res => {
              this.$message({
                message: '新增成功, 默认密码123456',
                type: 'success'
              })
              this.close()
              this.userData()
            })
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm(userForm) {
      this.$refs[userForm].resetFields()
    },
    getRoles() {
      getRoles().then(res => {
        this.rolesData = res.data.results
      })
    }
  }
}
</script>

<style scoped>

</style>
